/*     */ package com.zimbra.cs.mailbox.lock;
/*     */ 
/*     */ import java.util.concurrent.ConcurrentHashMap;
/*     */ import java.util.concurrent.ConcurrentMap;
/*     */ import java.util.concurrent.TimeUnit;
/*     */ import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
/*     */ import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class DebugZLock
/*     */   extends ZLock
/*     */ {
/*     */   private static final long serialVersionUID = -3009063384967180207L;
/*     */   private final DebugReentrantReadLock readLock;
/*     */   private final DebugReentrantWriteLock writeLock;
/*     */   
/*     */   public DebugZLock()
/*     */   {
/*  38 */     this.readLock = new DebugReentrantReadLock(this);
/*  39 */     this.writeLock = new DebugReentrantWriteLock(this);
/*     */   }
/*     */   
/*     */   public ReentrantReadWriteLock.ReadLock readLock()
/*     */   {
/*  44 */     return this.readLock;
/*     */   }
/*     */   
/*     */   public ReentrantReadWriteLock.WriteLock writeLock()
/*     */   {
/*  49 */     return this.writeLock;
/*     */   }
/*     */   
/*     */ 
/*     */   private class DebugReentrantReadLock
/*     */     extends ReentrantReadWriteLock.ReadLock
/*     */   {
/*     */     private static final long serialVersionUID = -2861690755318306713L;
/*  57 */     private final ConcurrentMap<Long, MailboxLockOwner> debugReadOwners = new ConcurrentHashMap();
/*     */     
/*     */     DebugReentrantReadLock(DebugZLock readWriteLock) {
/*  60 */       super();
/*     */     }
/*     */     
/*     */     void recordOwner() {
/*  64 */       Long key = Long.valueOf(Thread.currentThread().getId());
/*  65 */       MailboxLockOwner owner = (MailboxLockOwner)this.debugReadOwners.get(key);
/*  66 */       if (owner == null) {
/*  67 */         owner = new MailboxLockOwner();
/*  68 */         this.debugReadOwners.put(key, owner);
/*     */       }
/*  70 */       owner.increment();
/*     */     }
/*     */     
/*     */     void removeOwner() {
/*  74 */       Long key = Long.valueOf(Thread.currentThread().getId());
/*  75 */       MailboxLockOwner owner = (MailboxLockOwner)this.debugReadOwners.get(key);
/*  76 */       if (owner != null) {
/*  77 */         int count = owner.decrement();
/*  78 */         if (count <= 0) {
/*  79 */           this.debugReadOwners.remove(key);
/*     */         }
/*     */       }
/*     */     }
/*     */     
/*     */     public void lock()
/*     */     {
/*  86 */       super.lock();
/*  87 */       recordOwner();
/*     */     }
/*     */     
/*     */     public void lockInterruptibly() throws InterruptedException
/*     */     {
/*  92 */       super.lockInterruptibly();
/*  93 */       recordOwner();
/*     */     }
/*     */     
/*     */     public boolean tryLock()
/*     */     {
/*  98 */       if (super.tryLock()) {
/*  99 */         recordOwner();
/* 100 */         return true;
/*     */       }
/* 102 */       return false;
/*     */     }
/*     */     
/*     */     public boolean tryLock(long timeout, TimeUnit unit)
/*     */       throws InterruptedException
/*     */     {
/* 108 */       if (super.tryLock(timeout, unit)) {
/* 109 */         recordOwner();
/* 110 */         return true;
/*     */       }
/* 112 */       return false;
/*     */     }
/*     */     
/*     */ 
/*     */     public void unlock()
/*     */     {
/* 118 */       super.unlock();
/* 119 */       removeOwner();
/*     */     }
/*     */   }
/*     */   
/*     */   private class DebugReentrantWriteLock extends ReentrantReadWriteLock.WriteLock
/*     */   {
/*     */     private static final long serialVersionUID = -7969337036084655493L;
/*     */     
/*     */     DebugReentrantWriteLock(DebugZLock readWriteLock)
/*     */     {
/* 129 */       super();
/*     */     }
/*     */   }
/*     */   
/*     */   private ConcurrentMap<Long, MailboxLockOwner> getReadOwners() {
/* 134 */     return this.readLock.debugReadOwners;
/*     */   }
/*     */   
/*     */   public void printStackTrace(StringBuilder out)
/*     */   {
/* 139 */     Thread owner = getOwner();
/* 140 */     if (owner != null) {
/* 141 */       out.append("Write Lock Owner - ");
/* 142 */       printStackTrace(owner, out);
/*     */     }
/* 144 */     int readCount = getReadLockCount();
/* 145 */     if (readCount > 0) {
/* 146 */       out.append("Reader Count - " + readCount + "\n");
/*     */     }
/* 148 */     if (getReadOwners() != null) {
/* 149 */       for (MailboxLockOwner lockOwner : getReadOwners().values()) {
/* 150 */         out.append("Read Lock Owner (Holds: " + lockOwner.getCount() + ") - ");
/* 151 */         printStackTrace(lockOwner.getOwnerThread(), out);
/*     */       }
/*     */     }
/* 154 */     for (Thread waiter : getQueuedThreads()) {
/* 155 */       out.append("Lock Waiter - ");
/* 156 */       printStackTrace(waiter, out);
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mailbox/lock/DebugZLock.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */